
package id.isran.kamusindonesiamuna;

import id.isran.kamusindonesiamuna.adapter.FragmentAdapter;
import id.isran.kamusindonesiamuna.basisdata.Data;
import id.isran.kamusindonesiamuna.entity.E_Fragment;
import id.isran.kamusindonesiamuna.fragment.FragmentKalimatInaMuna;
import id.isran.kamusindonesiamuna.fragment.FragmentKalimatMunaIna;
import id.isran.kamusindonesiamuna.fragment.FragmentKataInaMuna;
import id.isran.kamusindonesiamuna.fragment.FragmentKataMunaIna;
import id.isran.kamusindonesiamuna.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import com.bugsense.trace.BugSenseHandler;
import com.viewpagerindicator.TitlePageIndicator;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;

/**
 * Class Activity yang pertama kali dijalankan didalam class ini terdapat baris
 * code yang menghandle fragment2 mana yang akan ditampilkan
 */
public class MenuUtamaActivity extends ActionBarActivity {

    private List<E_Fragment> listFragment;
    private FragmentAdapter adapter;
    private TitlePageIndicator titleViewPager;
    private ViewPager viewPager;

    private ArrayAdapter<String> adapterMunaIna = null;
    private String[] dataAutoCompleteMuna;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_utama);

        titleViewPager = (TitlePageIndicator) findViewById(R.id.title_indicator);
        viewPager = (ViewPager) findViewById(R.id.view_pager);

        /**
         * init bugsense, error reporting sistem
         */
        BugSenseHandler.initAndStartSession(this, "e55b7dec");

        /**
         * load data kamus, muna indonesia, indonesia muna
         */
        Data.initData(this, Data.MUNA_INDO);
        Data.initData(this, Data.INDO_MUNA);

        addFragment();

        adapter = new FragmentAdapter(this, getSupportFragmentManager(), listFragment);

        viewPager.setAdapter(adapter);
        titleViewPager.setViewPager(viewPager);
        titleViewPager.setClipPadding(-50);
        titleViewPager.setSelectedColor(Color.parseColor("#f26149"));
    }

    public ArrayAdapter<String> getAdapterMunaIna() {
        return adapterMunaIna;
    }

    private void loadData() {
        dataAutoCompleteMuna = new String[Data.BARIS_COUNT_MUNA_INA + 1];
        for (int i = 0; i < Data.BARIS_COUNT_MUNA_INA + 1; i++) {
            dataAutoCompleteMuna[i] = Data.dataMunaIna[i][1];
            Utils.TRACE("FragmentKataMunaIna", "loadData: " + i);
        }
    }

    public void setAdapterMunaIna(ArrayAdapter<String> adapterMunaIna) {
        this.adapterMunaIna = adapterMunaIna;
    }

    /**
     * menambahkan fragment di activity utama, fragment yang ditambahkan adalah
     * <p>
     * - Kata ina muna
     * </p>
     * <p>
     * - Kata muna ina
     * </p>
     * <p>
     * - Kalimat ina muna
     * </p>
     * <p>
     * - Kalimat muna ina
     * </p>
     */
    private void addFragment() {
        listFragment = new ArrayList<E_Fragment>();

        listFragment.add(new E_Fragment("Kata Ina-Muna", FragmentKataInaMuna.class.getName()));
        listFragment.add(new E_Fragment("Kata Muna-Ina", FragmentKataMunaIna.class.getName()));
        listFragment
                .add(new E_Fragment("Kalimat Ina-Muna", FragmentKalimatInaMuna.class.getName()));
        listFragment
                .add(new E_Fragment("Kalimat Muna-Ina", FragmentKalimatMunaIna.class.getName()));

    }

    /**
     * load item untuk context menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_utama, menu);
        return true;
    }

    /**
     * event listener untuk context menu
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about:
                Toast.makeText(this, "About", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
